package com.service.impl;

import com.bean.entity.Warehouse;
import com.bean.vo.PageAssistant;
import com.bean.vo.WarehouseVo;
import com.dao.WarehouseMapper;
import com.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {
    /* 自动注入 */
    @Autowired
    WarehouseMapper warehouseMapper;
    @Override
    public PageAssistant<WarehouseVo> queryAll(WarehouseVo warehouseVo,PageAssistant<WarehouseVo> pa) {
        //查询总记录数
        pa.setCount(warehouseMapper.selectCount(warehouseVo));
        //设置分页
        warehouseVo.setPageSize(pa.getPageSize());
        warehouseVo.setCurrPage(pa.getCurrPage());
        warehouseVo.setStartCode(pa.getStartCode());
        //设置页面内容
        pa.setItems(warehouseMapper.selectByPage(warehouseVo));
        return pa;
    }

    @Override
    public WarehouseVo query(Warehouse warehouse) throws Exception {
        //执行
        List<WarehouseVo> warehouseVos = warehouseMapper.select(warehouse);
        if(warehouseVos.size() == 1){
            return warehouseVos.get(0);
        }else{
            throw new Exception("抱歉，未能查询到相关信息");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(Warehouse warehouse) throws Exception {
        //执行
        int i = warehouseMapper.insert(warehouse);
        if(i == 1){
            return 1;
        }else{
            throw new Exception("新增失败!");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(Warehouse warehouse) throws Exception {
        //执行
        int i = warehouseMapper.delete(warehouse);
        if(i == 1){
            return 1;
        }else{
            throw new Exception("删除失败!");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modify(Warehouse warehouse) throws Exception {
        //执行
        int i = warehouseMapper.update(warehouse);
        if(i == 1){
            return 1;
        }else{
            throw new Exception("修改失败!");
        }
    }
}
